using System.Net;
using System.Net.Mail;
using MultiAgentsShared;

namespace MultiAgentsClient.Tools;

[GptToolSet("Email ToolSet", "")]
public class EmailToolProvider
{
    [GptFunc("Send an email using the specified SMTP server", true)]
    public static bool SendEmail(
        [GptFuncParam("Sender email address")] string fromEmail,
        [GptFuncParam("Recipient email address")] string toEmail,
        [GptFuncParam("Email subject")] string subject,
        [GptFuncParam("Email body content")] string body,
        [GptFuncParam("SMTP server address")] string smtpServer,
        [GptFuncParam("SMTP username")] string username,
        [GptFuncParam("SMTP authorization code or password")] string authorizationCode,
        [GptFuncParam("SMTP server port (default is 587)")] int port = 587)
    {
        try
        {
            MailMessage mailMessage = new()
            {
                From = new MailAddress(fromEmail),
                Subject = subject,
                Body = body,
                IsBodyHtml = true
            };
            mailMessage.To.Add(toEmail);

            SmtpClient smtpClient = new(smtpServer, port)
            {
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(username, authorizationCode)
            };

            smtpClient.Send(mailMessage);

            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
}
